package com.cmit.upay.monitor.service;

import com.cmit.upay.monitor.monitor.ActiveMQMonitor;
import com.cmit.upay.monitor.properties.ActiveMQProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class MonitorService {
    @Autowired
    private ActiveMQProperties activeMQProperties;
    @Autowired
    private ActiveMQMonitor activeMQMonitor;

    @Scheduled(cron = "${activemq_monitor_cron}")
    public void doService() {
        log.info("启动监控MQ队列");
        if (activeMQProperties.getUseJmx()) {
            log.info("启动JMX监控");
            for (String jmxUrl : activeMQProperties.getJmxUrlList()) {
                activeMQMonitor.monitorUseJmx(jmxUrl);
            }
        } else {
            log.info("启动jolokia监控");
            for (String adminUrl : activeMQProperties.getAdminUrlList()) {
                activeMQMonitor.monitorUseJolokia(adminUrl);
            }
        }


    }

    @Scheduled(cron = "${activemq_monitor_consumer_cron}")
    public void consumerInfoService() {
        if (activeMQProperties.getOutputConsumer()) {
            log.info("开始获取队列中的Consumer信息");
            for (String adminUrl : activeMQProperties.getAdminUrlList()) {
                activeMQMonitor.requestConsumersInfo(adminUrl);
            }
        }
    }
}
